package com.rq.rvlibrary;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;


/**
 * 继承此类，完成双层RecyclerView 嵌套控制
 * <p>
 * getChildLayoutId()                         ->  子Item布局xml文件
 * getChildRecyclerId()                       ->  内层RecyclerView 视图ID
 * getChildData(Par parentBean, int position) ->  内层RecyclerView 滑动数据
 * <p>
 * inflateLayoutId() 布局文件中为最外层 RecyclerView 的布局xml文件
 *
 * @param <Par> extends NestingViewHolder.Extendable
 * @param <Chi>
 */
public class NestingViewHolder<Par, Chi> extends
        BaseViewHolder<Par> {

    public interface Extendable {
        boolean isOpen();
    }

    BaseAdapter innerAdapter;
    NestingBuilder builder;

    public NestingViewHolder(final View itemView) {
        super(itemView);
    }

    @Override
    public int inflateLayoutId() {
        return builder != null ? builder.getOutLayoutId() : super.inflateLayoutId();
    }

    public void init(NestingBuilder builder) {
        this.builder = builder;
        LOG.e("NestingViewHolder", "45:" + (builder == null));
        innerAdapter = new BaseAdapter(itemView.getContext(), getChildLayoutId(), DRChildViewHolder.class, this);
        if (getChildRecyclerUtil(innerAdapter) != null) {
            getChildRecyclerUtil(innerAdapter).set2View((RecyclerView) itemView.findViewById(getCRID()));
        } else {
            new RecyclerUtil(innerAdapter).set2View((RecyclerView) itemView.findViewById(getCRID()));
        }
    }

    protected RecyclerUtil getChildRecyclerUtil(BaseAdapter adapter) {
        if (builder != null) {
            return builder.getChildRecyclerUtil(adapter);
        }
        return null;
    }

    @Override
    public final void fillData(BaseAdapter adapter, int position, Par parentBean) {
        super.fillData(adapter, position, parentBean);
        List item = getChildData(parentBean, position);
        boolean showChild;
        if (parentBean instanceof NestingViewHolder.Extendable) {
            showChild = item != null && item.size() > 0 && ((Extendable) parentBean).isOpen();
        } else {
            showChild = item != null && item.size() > 0;
        }

        getItemView(getCRID()).setTag(R.id.rv_dr_child_p_position, position);
        fillParentData(this, adapter, position, parentBean, innerAdapter, showChild);
        if (!showChild) {
            LOG.e("NestingViewHolder", "fillData.46:");
            getItemView(getCRID()).setVisibility(View.GONE);
            return;
        }
        LOG.e("NestingViewHolder", "fillData.49:" + item.size());
        getItemView(getCRID()).setVisibility(View.VISIBLE);
        innerAdapter.setData(item);
    }

    private int getCRID() {
        return builder == null ? getChildRecyclerId() : builder.getInnerRecyclerId();
    }

    /**
     * @param holder
     * @param position  holder 所处内列表位置
     * @param childBean
     * @param pPostion  holder 所处外列表位置
     */
    protected void fillChildData(EasyViewHolder holder, int position, Chi childBean, int pPostion) {
        if (builder != null) {
            builder.fillChildData(holder, position, childBean, pPostion);
        }
    }

    protected void fillParentData(BaseViewHolder holder, BaseAdapter parentAdapter, int position,
                                  Par parentBean, BaseAdapter childAdapter, boolean showChild) {
        if (builder != null) {
            builder.fillParentData(holder, parentAdapter, position, parentBean, childAdapter, showChild);
        }
    }

    protected List getChildData(Par parentBean, int position) {
        if (builder != null) {
            return builder.getChildData(parentBean, position);
        }
        return null;
    }

    protected int getChildLayoutId() {
        if (builder != null) {
            return builder.getInnerLayoutId();
        }
        return 0;
    }

    protected int getChildRecyclerId() {
        if (builder != null) {
            return builder.getInnerRecyclerId();
        }
        return 0;
    }

    protected ViewGroup.LayoutParams getChildLayoutParams(@NonNull ViewGroup.LayoutParams oldLM) {
        if (builder != null) {
            return builder.getChildLayoutParams(oldLM);
        }
        return oldLM;
    }

    protected void initChildView(BaseViewHolder itemView) {

    }

    public class DRChildViewHolder extends EasyViewHolder<Chi> {
        public DRChildViewHolder(View itemView) {
            super(itemView);
            initChildView(DRChildViewHolder.this);
        }

        @Override
        public ViewGroup.LayoutParams getLMLayoutParams(@NonNull ViewGroup.LayoutParams oldLM) {
            return getChildLayoutParams(oldLM);
        }

        @Override
        public void fillData(BaseAdapter adapter, int position, Chi o) {
            super.fillData(adapter, position, o);
            int pPosition = (int) getParentView().getTag(R.id.rv_dr_child_p_position);
            fillChildData(DRChildViewHolder.this, position, o, pPosition);
        }
    }

}
